package com.tvunetworks.device.extra.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.device.extra.annotation.Logined;
import com.tvunetworks.device.extra.model.param.CheckSourceParam;
import com.tvunetworks.device.extra.common.model.PageDeviceParam;
import com.tvunetworks.device.extra.model.param.RtmpSourceParam;
import com.tvunetworks.device.extra.service.SourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author Etan Yang
 * @create: 2021/11/22 20:15
 */

@RestController
@RequestMapping("source")
public class SourceController {

    @Autowired
    private SourceService sourceService;

    /**
     * Paging query source
     *
     * @param loginUser loginUser
     * @param param     pageDeviceParam
     * @return Paging query source
     */
    @PostMapping("page")
    public Result pageSource(@Logined LoginUser loginUser, @Validated @RequestBody PageDeviceParam param) {
        String userId = StringUtils.isNotEmpty(param.getUserId()) ? param.getUserId() : loginUser.getId();
        param.setUserId(userId);
        return ResultUtil.success(sourceService.pageSource(param));
    }

    /**
     * 根据sourceId获取source的详细信息
     * @param peerId sourceId
     * @return source的详细信息
     */
    @PostMapping("getById")
    public Result getById(@RequestParam("peerId") String peerId) {
        return ResultUtil.success(sourceService.getById(peerId));
    }

    /**
     * 校验source是否正在使用
     * @param param 参数
     * @return 返回结果
     */
    @PostMapping("checkSource")
    public Result<String> checkSource(@RequestBody @Validated CheckSourceParam param) {
        return ResultUtil.success(sourceService.checkSource(param));
    }

    /**
     * 获取rtmp的url
     * @param param 参数
     * @return rtmp的url
     */
    @PostMapping("getRtmpServerUrl")
    public Result<String> getRtmpServerUrl(@RequestBody @Validated RtmpSourceParam param) {
        return ResultUtil.success(sourceService.getRtmpUrl(param));
    }

    /**
     * 获取rtmp的key
     * @param loginUser 用户信息
     * @param param 参数
     * @return rtmp的key
     */
    @PostMapping(value = "getRtmpKey", name = "获取rtmp key")
    public Result<String> getRtmpKey(@Logined LoginUser loginUser, @RequestBody @Validated RtmpSourceParam param) {
        String userId = StringUtils.isNotEmpty(param.getUserId()) ? param.getUserId() : loginUser.getId();
        param.setUserId(userId);
        return ResultUtil.success(sourceService.getRtmpKey(param));
    }
}
